<template>
  <div id="newList">
    <!-- 搜索区域 -->
    <div id="newSearch">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="">
          <el-input
            v-model="formInline.user"
            placeholder="请输入搜索内容"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search">搜索</el-button>
          <el-button type="primary" @click="addShowDialog" icon="el-icon-plus"
            >添加</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <!-- 弹窗区域 -->
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="80%"
      top="20px"
      :before-close="handleClose"
    >
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addNewsSubmit">确 定</el-button>
      </span>
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="新闻标题" prop="title">
          <el-input
            v-model="form.title"
            placeholder="请输入新闻标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="新闻作者" prop="author">
          <el-input
            v-model="form.author"
            placeholder="请输入新闻作者"
          ></el-input>
        </el-form-item>
        <el-form-item label="发布时间">
          <el-col :span="11">
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="form.date"
              style="width: 100%"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="是否上线">
          <el-switch v-model="form.publish"></el-switch>
        </el-form-item>
        <el-form-item label="新闻内容">
          <quill-editor v-model="form.content" ref="myQuillEditor">
          </quill-editor>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 表格显示区域 -->
    <el-table :data="tableData" border>
      <el-table-column prop="title" label="新闻标题"> </el-table-column>
      <el-table-column prop="author" label="作者"> </el-table-column>
      <el-table-column prop="time" label="发布时间"> </el-table-column>
      <el-table-column label="操作">
        <el-button type="primary" icon="el-icon-edit">编辑</el-button>
        <el-button type="danger" icon="el-icon-delete">删除</el-button>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div id="Page">
      <el-pagination
  background
  layout="prev, pager, next"
  :total="totalPage" :page-size="limit" @current-change="currentPage">
</el-pagination>
    </div>
  </div>
</template>

<script>
import { addNewsApi, getNewsApi } from "../api/news";
export default {
  name: "newsList",
  data() {
    return {
      // 顶部搜索表单数据
      formInline: {
        user: "",
        region: "",
      },

      // 最大页数
				totalPage: 0,
				// 第几页
				page: 1,
				// 每页数量
				limit: 6,

      // 弹窗显示条件
      dialogVisible: false,

      // 弹窗添加新闻表单数据
      form: {
        title: "",
        author: "",
        date: "",
        publish: true,
        content: "",
      },

      // 表单验证
      rules: {
        title: [
          { required: true, message: "请输入新闻的标题", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "长度在 1 到 50 个字符",
            trigger: "blur",
          },
        ],
        author: [
          { required: true, message: "请输入新闻的作者", trigger: "blur" },
          {
            min: 1,
            max: 10,
            message: "长度在 1 到 10 个字符",
            trigger: "blur",
          },
        ],
      },

      // 表格数据
      tableData: [
        // {
        //   title: "民航局回应东航坠机传言：谣言",
        //   author: "抽屉视频",
        //   time: "2022年4月11日",
        // },
        // {
        //   title: "《加快建设全国统一大市场的意见》发布 解读来了",
        //   author: "央视新闻",
        //   time: "2022-04-11 10:36",
        // },
      ],
    };
  },
  methods: {
    // 弹窗的关闭
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },

    // 弹窗确定按钮
    addNewsSubmit() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          let res = await addNewsApi({
            data: this.form,
          });
          if (res.code == 200) {
            this.$message({
              message: "添加成功！",
              type: "success",
            });
            this.dialogVisible = false;
            this.showNewsList()
          } else {
            this.$message.error("添加失败！");
          }
        }
      });
    },

    // 添加新闻按钮
    addShowDialog() {
      // 初始化弹窗
      this.form = {
        title: "",
        author: "",
        time: "",
        isOnline: "1",
        content: "",
      };
      this.dialogVisible = true;
    },

    async showNewsList() {
				let res = await getNewsApi({
					params: {
						page: this.page,
						limit: this.limit
					}
				});
				// console.log(res)
				this.tableData = res.data;
				this.totalPage = res.count;
				// console.log(this.totalPage)
			},
			currentPage(page){
				// console.log(page)
				this.page = page;
				this.showNewsList()
			},
   
    
  },
  async mounted() {
    this.showNewsList();
    },
};
</script>

<style lang='scss'>
#newList {
  margin-top: 20px;
  margin-left: 10px;
  #Page {
    margin-top: 20px;
    text-align: right;
  }
}
</style>